package de.koller.planet2D.frame.uv;

import de.koller.planet2D.container.TextureValue;
import de.koller.planet2D.frame.ObjectFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class TexturePreview extends JPanel {
	
	private final ObjectFrame objectFrame;

	public TexturePreview() {
		this( null );
	}

	public TexturePreview( ObjectFrame objectFrame ) {
		this.objectFrame = objectFrame;
	}

	@Override
	protected void paintComponent( Graphics g ) {
		super.paintComponent( g );
		
		if( objectFrame != null && objectFrame.renderObj.getTextureEntity() != null )
			renderPreview( (Graphics2D)g );
	}
	
	private void renderPreview( Graphics2D g ) {
		TextureValue t = objectFrame.renderObj.getTextureEntity().basicTexture;
		if( t == null )
			return;
		
		double scale = 1;
		scale = Math.min( (double)this.getWidth() / (double)t.w, (double)this.getHeight() / (double)t.h );

		g.drawImage( objectFrame.proot.renderContainer.textur,
				0, 0,
				(int)(t.w * scale),
				(int)(t.h * scale),
				t.x, t.y, t.x+t.w, t.y+t.h, null );
	}
	
}
